package com.cn.jinl.service;

import com.cn.jinl.api.IOnDutyPersonService;
import com.cn.jinl.dao.OnDutyPersonMapper;
import com.cn.jinl.domain.OnDutyPerson;
import com.cn.jinl.domain.OnDutyPersonExample;
import com.cn.jinl.utils.CommonUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service
public class OnDutyPersonServiceImpl implements IOnDutyPersonService {

    @Autowired
    private OnDutyPersonMapper onDutyPersonMapper;

    @Override
    public List<OnDutyPerson> getOnDutyPersonList() throws Exception {
        OnDutyPersonExample example  = new OnDutyPersonExample();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String day = simpleDateFormat.format(new Date());
        example.createCriteria().andDutyDayEqualTo(day);
        example.setOrderByClause("white_day asc");
        List<OnDutyPerson> list = onDutyPersonMapper.selectByExample(example);
        return list;
    }

    @Override
    public String getOnDutyPersonByDutyAndWhite(String dutyDay, String whiteDay) throws Exception {
        if(StringUtils.isEmpty(dutyDay)||StringUtils.isEmpty(whiteDay)){
            return "";
        }
        OnDutyPersonExample example  = new OnDutyPersonExample();
        example.createCriteria().andDutyDayEqualTo(dutyDay).andWhiteDayEqualTo(whiteDay);
        List<OnDutyPerson> list = onDutyPersonMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(list)){
            return "";
        }
        return list.get(0).getUserName();
    }

    @Override
    public String getOnDutyLockedByDutyAndWhite(String dutyDay, String whiteDay) throws Exception {
        if(StringUtils.isEmpty(dutyDay)||StringUtils.isEmpty(whiteDay)){
            return "";
        }
        OnDutyPersonExample example  = new OnDutyPersonExample();
        example.createCriteria().andDutyDayEqualTo(dutyDay).andWhiteDayEqualTo(whiteDay);
        List<OnDutyPerson> list = onDutyPersonMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(list)){
            return "";
        }
        return list.get(0).getLocked();
    }

    @Override
    public String addOnDutyPerson(String dutyDay) throws Exception {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if(StringUtils.isEmpty(dutyDay)){
            dutyDay = simpleDateFormat.format(date);
        }
        OnDutyPersonExample example  = new OnDutyPersonExample();
        example.createCriteria().andDutyDayEqualTo(dutyDay);

        int count  = onDutyPersonMapper.countByExample(example);
        if(count == 0){
            List<OnDutyPerson> inserts = Lists.newArrayList();
            OnDutyPerson whiteDay = new OnDutyPerson();
            whiteDay.setDutyDay(dutyDay);
            whiteDay.setId(CommonUtil.generateRandomNum("onduty-"));
            whiteDay.setTimeRange("6:50-18:20");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            whiteDay.setWeek(CommonUtil.convertToWeekday(dayOfWeek));
            whiteDay.setWhiteDay("1");
            inserts.add(whiteDay);

            OnDutyPerson blackDay = whiteDay.clone();
            blackDay.setTimeRange("18:20-6:50");
            blackDay.setWhiteDay("2");
            blackDay.setId(CommonUtil.generateRandomNum("onduty-"));
            inserts.add(blackDay);

            count = onDutyPersonMapper.batchInsertOnDutyPersons(inserts);
        }
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateOnDutyPerson(OnDutyPerson onDutyPerson) throws Exception {
        String id = onDutyPerson.getId();
        if(StringUtils.isEmpty(id)){
            return "主键为空！";
        }
        OnDutyPersonExample example  = new OnDutyPersonExample();
        example.createCriteria().andIdEqualTo(id);
        int count = onDutyPersonMapper.updateByExampleSelective(onDutyPerson,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateOnDutyPersonLocked(String dutyDay,String whiteDay) throws Exception {
        OnDutyPerson update = new OnDutyPerson();
        update.setWhiteDay(whiteDay);
        update.setDutyDay(dutyDay);
        int count = onDutyPersonMapper.updateOnDutyPersonLockedByWhiteDay(update);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateOnDutyPersonByDay(String userId, String userName, String dutyDay, String whiteDay) throws Exception {
        if(StringUtils.isNotEmpty(dutyDay)&&StringUtils.isNotEmpty(whiteDay)){
            OnDutyPersonExample example = new OnDutyPersonExample();
            example.createCriteria().andWhiteDayEqualTo(whiteDay).andDutyDayEqualTo(dutyDay);
            OnDutyPerson update = new OnDutyPerson();
            update.setUserId(userId);
            update.setUserName(userName);
            int count = onDutyPersonMapper.updateByExampleSelective(update,example);
            return CommonUtil.outStr(count);
        }
        return "";
    }

    @Override
    public String deleteOnDutyPersonByDay(String dutyDay) throws Exception {
        OnDutyPersonExample example  = new OnDutyPersonExample();
        example.createCriteria().andDutyDayEqualTo(dutyDay);
        int count = onDutyPersonMapper.deleteByExample(example);
        return CommonUtil.outStr(count);
    }

}
